package com.lambkit.module.upms.server.api.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.lambkit.Lambkit;
import com.lambkit.common.service.LambkitApiService;
import com.lambkit.common.service.LambkitService;
import com.lambkit.component.shiro.processer.AuthorizeResult;
import com.lambkit.db.sql.IQuery;
import com.lambkit.db.sql.column.Example;
import com.lambkit.module.upms.api.*;
import com.lambkit.module.upms.model.UpmsLogModel;
import com.lambkit.module.upms.model.UpmsRoleModel;
import com.lambkit.module.upms.model.UpmsUserModel;
import com.lambkit.module.upms.record.*;
import com.lambkit.module.upms.server.UpmsServerManager;
import com.lambkit.module.upms.server.service.UpmsServerApiService;
import com.lambkit.common.service.Service;
import com.lambkit.module.upms.sql.UpmsRolePermissionSqlBuilder;
import com.lambkit.module.upms.sql.UpmsUserPermissionSqlBuilder;

import java.lang.annotation.Annotation;
import java.util.List;

@Service
@LambkitService
public class UpmsApiServiceImpl implements UpmsApiService {
	private static Log _log = LogFactory.get(UpmsApiServiceImpl.class);

	private UpmsServerApiService serverApiService;


	protected UpmsServerApiService getServerApiService() {
		if(serverApiService==null) {
			serverApiService = Lambkit.get(UpmsServerApiService.class);
		}
		return serverApiService;
	}
	
	@Override
	public AuthorizeResult invoke(String target, Annotation[] annotations) {
		// TODO Auto-generated method stub
		return getServerApiService().invoke(target, annotations);
	}

	@Override
	public UpmsPermission selectOrCreateUpmsPermission(UpmsPermission permission) {
		String uri = permission.getUri();
		LambkitApiService<UpmsPermission> apiService = UpmsServerManager.me().getLambkitApiService(UpmsPermission.class);
		String tableName = apiService.getTableName();
		UpmsPermission upmsPermission = apiService.findFirstByCache(tableName, uri, "select * from " + tableName + " where uri=?", uri);
		if(upmsPermission==null) {
			apiService.save(permission);
		}
		return permission;
	}

	@Override
	public List<UpmsPermission> selectUpmsPermissionByUpmsUserId(Long upmsUserId) {
		// TODO Auto-generated method stub
		UpmsUser upmsUser = UpmsServerManager.me().getLambkitApiService(UpmsUser.class).findByPrimaryKey(upmsUserId);
		if (null == upmsUser || 1 == upmsUser.getLocked()) {
			_log.info("selectUpmsPermissionByUpmsUserId : upmsUserId={}", upmsUserId);
			return null;
		}
		return UpmsServerManager.me().getLambkitApiService(UpmsPermission.class).find(UpmsServerManager.me().getSqlPara("upms.selectUpmsPermissionByUpmsUserId", upmsUserId));
//		List<UpmsPermissionModel> lists = getServerApiService().selectUpmsPermissionByUpmsUserId(upmsUserId);
//		return Lists.transform(lists, new Function<UpmsPermissionModel, UpmsPermission>() {
//			@Override
//			public UpmsPermission apply(UpmsPermissionModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}


	@Override
	public List<UpmsPermission> selectUpmsPermissionByUpmsUserIdByCache(Long upmsUserId) {
		// TODO Auto-generated method stub
		UpmsUser upmsUser = UpmsServerManager.me().getLambkitApiService(UpmsUser.class).findByPrimaryKey(upmsUserId);
		if (null == upmsUser || 1 == upmsUser.getLocked()) {
			_log.info("selectUpmsPermissionByUpmsUserIdByCache : upmsUserId={}", upmsUserId);
			return null;
		}
		List<UpmsPermission> upmsPermissionList = Lambkit.getCache().get("upmsCache", "selectUpmsPermissionByUpmsUserId_pojo_" + upmsUserId);
		if(upmsPermissionList==null) {
			upmsPermissionList = selectUpmsPermissionByUpmsUserId(upmsUserId);
			Lambkit.getCache().put("upmsCache", "selectUpmsPermissionByUpmsUserId_pojo_" + upmsUserId, upmsPermissionList);
		}
		return upmsPermissionList;
//		List<UpmsPermissionModel> lists = getServerApiService().selectUpmsPermissionByUpmsUserIdByCache(upmsUserId);
//		return Lists.transform(lists, new Function<UpmsPermissionModel, UpmsPermission>() {
//			@Override
//			public UpmsPermission apply(UpmsPermissionModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsRole> selectUpmsRoleByUpmsUserId(Long upmsUserId) {
		// TODO Auto-generated method stub
		UpmsUser upmsUser = UpmsServerManager.me().getLambkitApiService(UpmsUser.class).findByPrimaryKey(upmsUserId);
		if (null == upmsUser || 1 == upmsUser.getLocked()) {
			_log.info("selectUpmsRoleByUpmsUserId : upmsUserId={}", upmsUserId);
			return null;
		}
		return UpmsServerManager.me().getLambkitApiService(UpmsRole.class).find(UpmsServerManager.me().getSqlPara("upms.selectUpmsRoleByUpmsUserId", upmsUserId));
//		List<UpmsRoleModel> lists = getServerApiService().selectUpmsRoleByUpmsUserId(upmsUserId);
//		return Lists.transform(lists, new Function<UpmsRoleModel, UpmsRole>() {
//			@Override
//			public UpmsRole apply(UpmsRoleModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsRole> selectUpmsRoleByUpmsUserIdByCache(Long upmsUserId) {
		// TODO Auto-generated method stub
		UpmsUser upmsUser = UpmsServerManager.me().getLambkitApiService(UpmsUser.class).findByPrimaryKey(upmsUserId);
		if (null == upmsUser || 1 == upmsUser.getLocked()) {
			_log.info("selectUpmsRoleByUpmsUserIdByCache : upmsUserId={}", upmsUserId);
			return null;
		}
		List<UpmsRole> upmsRoleList = Lambkit.getCache().get("upmsCache", "selectUpmsRoleByUpmsUserId_pojo_" + upmsUserId);
		if(upmsRoleList==null) {
			upmsRoleList = selectUpmsRoleByUpmsUserId(upmsUserId);
			Lambkit.getCache().put("upmsCache", "selectUpmsRoleByUpmsUserId_pojo_" + upmsUserId, upmsRoleList);
		}
		return upmsRoleList;
//		List<UpmsRoleModel> lists = getServerApiService().selectUpmsRoleByUpmsUserIdByCache(upmsUserId);
//		return Lists.transform(lists, new Function<UpmsRoleModel, UpmsRole>() {
//			@Override
//			public UpmsRole apply(UpmsRoleModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsRolePermission> selectUpmsRolePermisstionByUpmsRoleId(Long upmsRoleId) {
		// TODO Auto-generated method stub
		Example upmsRolePermissionExample = UpmsRolePermissionSqlBuilder.create().andRoleIdEqualTo(upmsRoleId).example();
		return UpmsServerManager.me().getLambkitApiService(UpmsRolePermission.class).find(upmsRolePermissionExample);
//		List<UpmsRolePermissionModel> lists = getServerApiService().selectUpmsRolePermisstionByUpmsRoleId(upmsRoleId);
//		return Lists.transform(lists, new Function<UpmsRolePermissionModel, UpmsRolePermission>() {
//			@Override
//			public UpmsRolePermission apply(UpmsRolePermissionModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	

	@Override
	public List<UpmsUserPermission> selectUpmsUserPermissionByUpmsUserId(Long upmsUserId) {
		// TODO Auto-generated method stub
		Example upmsUserPermissionExample = UpmsUserPermissionSqlBuilder.create()
				.andUserIdEqualTo(upmsUserId).example();
		return UpmsServerManager.me().getLambkitApiService(UpmsUserPermission.class).find(upmsUserPermissionExample);
//		List<UpmsUserPermissionModel> lists = getServerApiService().selectUpmsUserPermissionByUpmsUserId(upmsUserId);
//		return Lists.transform(lists, new Function<UpmsUserPermissionModel, UpmsUserPermission>() {
//			@Override
//			public UpmsUserPermission apply(UpmsUserPermissionModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsSystem> selectUpmsSystemByExample(Example upmsSystemExample) {
		// TODO Auto-generated method stub
		return UpmsServerManager.me().getLambkitApiService(UpmsSystem.class).find(upmsSystemExample);
//		List<UpmsSystemModel> lists = getServerApiService().selectUpmsSystemByExample(upmsSystemExample);
//		return Lists.transform(lists, new Function<UpmsSystemModel, UpmsSystem>() {
//			@Override
//			public UpmsSystem apply(UpmsSystemModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsSystem> selectUpmsSystemByQuery(IQuery paras) {
		// TODO Auto-generated method stub
		return UpmsServerManager.me().getLambkitApiService(UpmsSystem.class).find(paras);
//		List<UpmsSystemModel> lists = getServerApiService().selectUpmsSystemByQuery(paras);
//		return Lists.transform(lists, new Function<UpmsSystemModel, UpmsSystem>() {
//			@Override
//			public UpmsSystem apply(UpmsSystemModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsOrganization> selectUpmsOrganizationByUpmsUserId(Long upmsUserId) {
		// TODO Auto-generated method stub
		UpmsUser upmsUser = UpmsServerManager.me().getLambkitApiService(UpmsUser.class).findByPrimaryKey(upmsUserId);
		if (null == upmsUser || 1 == upmsUser.getLocked()) {
			_log.info("selectUpmsRoleByUpmsUserId : upmsUserId={}", upmsUserId);
			return null;
		}
		return UpmsServerManager.me().getLambkitApiService(UpmsOrganization.class).find(UpmsServerManager.me().getSqlPara("upms.selectUpmsOrganizationByUpmsUserId", upmsUserId));
//		List<UpmsOrganizationModel> lists = getServerApiService().selectUpmsOrganizationByUpmsUserId(upmsUserId);
//		return Lists.transform(lists, new Function<UpmsOrganizationModel, UpmsOrganization>() {
//			@Override
//			public UpmsOrganization apply(UpmsOrganizationModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsOrganization> selectUpmsOrganizationByExample(Example upmsOrganizationExample) {
		// TODO Auto-generated method stub
		return UpmsServerManager.me().getLambkitApiService(UpmsOrganization.class).find(upmsOrganizationExample);
//		List<UpmsOrganizationModel> lists = getServerApiService().selectUpmsOrganizationByExample(upmsOrganizationExample);
//		return Lists.transform(lists, new Function<UpmsOrganizationModel, UpmsOrganization>() {
//			@Override
//			public UpmsOrganization apply(UpmsOrganizationModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public List<UpmsOrganization> selectUpmsOrganizationByQuery(IQuery paras) {
		// TODO Auto-generated method stub
		return UpmsServerManager.me().getLambkitApiService(UpmsOrganization.class).find(paras);
//		List<UpmsOrganizationModel> lists = getServerApiService().selectUpmsOrganizationByQuery(paras);
//		return Lists.transform(lists, new Function<UpmsOrganizationModel, UpmsOrganization>() {
//			@Override
//			public UpmsOrganization apply(UpmsOrganizationModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public UpmsUser selectUpmsUserByUsername(String username) {
		// TODO Auto-generated method stub
		UpmsUserModel model = getServerApiService().selectUpmsUserByUsername(username);
		return model.toPojo();
	}

	@Override
	public UpmsUser selectUpmsUserByEmail(String email) {
		// TODO Auto-generated method stub
		UpmsUserModel model = getServerApiService().selectUpmsUserByEmail(email);
		return model.toPojo();
	}

	@Override
	public UpmsUser selectUpmsUserByPhone(String phone) {
		// TODO Auto-generated method stub
		UpmsUserModel model = getServerApiService().selectUpmsUserByPhone(phone);
		return model.toPojo();
	}

	@Override
	public UpmsUser selectUpmsUserByLoginName(String LoginName) {
		// TODO Auto-generated method stub
		UpmsUserModel model = getServerApiService().selectUpmsUserByLoginName(LoginName);
		return model.toPojo();
	}

	@Override
	public UpmsUser selectUpmsUserByUserId(Long upmsUserId) {
		UpmsUserModel model = getServerApiService().selectUpmsUserByUserId(upmsUserId);
		return model.toPojo();
	}

	@Override
	public int insertUpmsLogSelective(UpmsLog record) {
		// TODO Auto-generated method stub
		int result = getServerApiService().insertUpmsLogSelective(UpmsLogModel.create(record));
		return result;
	}


	@Override
	public List<UpmsUser> selectUpmsUserByRoleId(Long id) {
		return UpmsServerManager.me().getLambkitApiService(UpmsUser.class).find(UpmsServerManager.me().getSqlPara("upms.selectUpmsUserByRoleId", id));
//		List<UpmsUserModel> lists = getServerApiService().selectUpmsUserByRoleId(id);
//		return Lists.transform(lists, new Function<UpmsUserModel, UpmsUser>() {
//			@Override
//			public UpmsUser apply(UpmsUserModel input) {
//				// TODO Auto-generated method stub
//				return input.toPojo();
//			}
//		});
	}

	@Override
	public UpmsRole selectUpmsRoleByRoleId(Long roleId) {
		// TODO Auto-generated method stub
		UpmsRoleModel model = getServerApiService().selectUpmsRoleByRoleId(roleId);
		return model.toPojo();
	}

	@Override
	public Boolean hasRole(Long userid, Long roleid) {
		return getServerApiService().hasRole(userid, roleid);
	}

	@Override
	public Boolean hasRole(Long userid, String roleName) {
		return getServerApiService().hasRole(userid, roleName);
	}

	@Override
	public Boolean lacksRole(String roleName) {
		return getServerApiService().lacksRole(roleName);
	}

	@Override
	public Boolean hasAnyRoles(Long userid, String roleNames) {
		return getServerApiService().hasAnyRoles(userid, roleNames);
	}

	@Override
	public Boolean hasAllRoles(Long userid, String roleNames) {
		return getServerApiService().hasAllRoles(userid, roleNames);
	}

	@Override
	public Boolean hasRule(Long userid, Long ruleid) {
		return getServerApiService().hasRule(userid, ruleid);
	}

	@Override
	public Boolean hasRule(Long userid, String permission) {
		return getServerApiService().hasRule(userid, permission);
	}

	@Override
	public Boolean lacksRule(String permission) {
		return getServerApiService().lacksRule(permission);
	}

	@Override
	public Boolean hasAnyRules(Long userid, String permissions) {
		return getServerApiService().hasAnyRules(userid, permissions);
	}

	@Override
	public Boolean hasAllRules(Long userid, String permissions) {
		return getServerApiService().hasAllRules(userid, permissions);
	}

	@Override
	public List<UpmsUserRole> selectUpmsUserRoleByUpmsUserIdByCache(Long upmsUserId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long selectUpmsSystemCount(Example upmsSystemExample) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UpmsUserOrganization> selectUpmsUserOrganizationByUpmsUserId(Long upmsUserId) {
		// TODO Auto-generated method stub
		return null;
	}

}
